package com.team9.fitness.mapper;

import com.team9.fitness.entity.FoodInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 食物信息映射器
 */
@Mapper
public interface FoodInfoMapper {

    @Insert("INSERT INTO food_info (name, calories, carbohydrates, protein, fat, category, created_at, updated_at) " +
            "VALUES (#{name}, #{calories}, #{carbohydrates}, #{protein}, #{fat}, #{category}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(FoodInfo foodInfo);

    @Select("SELECT * FROM food_info WHERE id = #{id}")
    FoodInfo selectById(Long id);

    @Select("SELECT * FROM food_info ORDER BY name")
    List<FoodInfo> selectAll();

    @Select("SELECT * FROM food_info WHERE category = #{category} ORDER BY name")
    List<FoodInfo> selectByCategory(String category);

    @Select("SELECT * FROM food_info WHERE name LIKE CONCAT('%', #{keyword}, '%') ORDER BY name")
    List<FoodInfo> searchByName(String keyword);

    @Select("SELECT DISTINCT category FROM food_info ORDER BY category")
    List<String> selectAllCategories();

    @Update("UPDATE food_info SET name = #{name}, calories = #{calories}, carbohydrates = #{carbohydrates}, " +
            "protein = #{protein}, fat = #{fat}, category = #{category}, updated_at = NOW() WHERE id = #{id}")
    int update(FoodInfo foodInfo);

    @Delete("DELETE FROM food_info WHERE id = #{id}")
    int deleteById(Long id);
}

